package com.space.school.controller;

import com.space.school.service.RegisterService;
import com.space.school.service.impl.RegisterServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "registerServlet",value = "/registerServlet")
public class RegisterServlet extends HttpServlet {
    private RegisterService registerService = new RegisterServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("register.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String name = req.getParameter("userName");
        String password = req.getParameter("passWord");
        int flag = registerService.register(name,password);
        if(flag==3){
            resp.sendRedirect("login.jsp");
        }else if(flag==1){
            req.getSession().setAttribute("login_message","你的账户已存在,请登录");
            req.getRequestDispatcher("login.jsp").forward(req,resp);
        }else if(flag==2){
            req.getSession().setAttribute("register_message","你的密码设置太简单了,请重新注册");
            req.getRequestDispatcher("register.jsp").forward(req,resp);
        }
    }
}

